package com.breeze.components.log.provider.json;

import ch.qos.logback.classic.spi.ILoggingEvent;
import net.logstash.logback.fieldnames.LogstashFieldNames;
import org.slf4j.MDC;

/**
 * @author breeze
 */
public class RequestDurationJsonProvider extends BaseJsonProvider {

  static String MDC_KEY = "mdc_duration";

  @Override
  public void setFieldNames(LogstashFieldNames fieldNames) {
    this.setFieldName("duration");
  }

  @Override
  public String getValue(ILoggingEvent event) {
    String duration = MDC.get(MDC_KEY);
    if (duration == null) {
      MDC.put(MDC_KEY, String.valueOf(System.currentTimeMillis()));
      return "0";
    }
    return String.valueOf(System.currentTimeMillis() - Long.parseLong(duration));
  }
}
